﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ICDA.Web.Models;
using ICDA.Models;
using ICDA.Repositories.ICDA;
using ICDA.Models.Entity;
using ICDA.Web.Helper;

namespace ICDA.Web.Controllers
{
    public class LeaveController : Controller
    {
        private static LeaveModel _LeaveModel;
        IRepository _repository;
        DatetTimeHelper dtHelper = new DatetTimeHelper();
        public static LeaveModel leaveModel
        {
            get
            {
                if (_LeaveModel == null)
                {
                    _LeaveModel = new LeaveModel();
                }
                return _LeaveModel;
            }
        }
        #region Constructors
        public LeaveController()
            : this(new Repository())
        {
        }
        public LeaveController(IRepository repository)
        {
            _repository = repository;
        }
        #endregion
        //
        // GET: /LeaveManagement/
        
        public ActionResult Index()
        {
            return View();
        }

        //GET: /LeaveManagement/
        public ActionResult LeaveApply()
        {
            if (Session["UID"] != null)
            {
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }
        [HttpPost]
        public ActionResult LeaveApply(LeaveModel model)
        {
            if (Session["UID"] != null)
            {
                string uid = Session["UID"].ToString().Trim();
                LeaveTable leave = new LeaveTable
                {
                    UserID = uid,
                    LeaveStartDate = dtHelper.FormatDateTime(model.LeaveStartDate),
                    LeaveEndDate = dtHelper.FormatDateTime(model.LeaveEndDate.Date),
                    LeaveType = model.LeaveType?"Leave":"Training",
                    LeaveName = model.LeaveName.Trim(),
                    DayType = model.DayType?"FullDay":"HalfDay",
                };
                _repository.AddLeave(leave);
                if (_repository.SaveChanges())
                {
                    return RedirectToAction("ViewLeave");
                }
            }
            return View(model);
            
        }

        public ActionResult ViewLeave()
        {
            if (Session["UID"] != null)
            {
                string uid = Session["UID"].ToString().Trim();
                List<LeaveModel> lm = new List<LeaveModel>();
                IEnumerable<LeaveTable> leavetab = _repository.leavetable.Where(u => u.UserID == uid).ToList();
                foreach (LeaveTable items in leavetab)
                {
                    LeaveModel lmodel = new LeaveModel();
                    lmodel.ID = items.ID;
                    lmodel.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveStartDate));
                    lmodel.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveEndDate));
                    lmodel.LeaveName = items.LeaveName.Trim();
                    lmodel.LeaveType =string.Equals(items.LeaveType.Trim(),"Leave");
                    lmodel.DayType = string.Equals(items.DayType.Trim(), "FullDay");
                    lm.Add(lmodel);
                }
                return View(lm);
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }

        [HttpGet]
        public ActionResult LeaveEdit(int id)
        {
            
            if (Session["UID"] != null)
            {
                LeaveTable leave = _repository.leavetable.Where(u => u.ID == id).FirstOrDefault();
                leaveModel.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", leave.LeaveStartDate));
                leaveModel.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", leave.LeaveEndDate));
                leaveModel.LeaveName = leave.LeaveName.Trim();
                leaveModel.DayType = string.Equals(leave.DayType.Trim(), "FullDay");
                leaveModel.LeaveType = string.Equals(leave.LeaveType.Trim(), "Leave");
                leaveModel.UserID = Session["UID"].ToString().Trim();
                return View(leaveModel);
            }
            
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }

        [HttpPost]
        public ActionResult LeaveEdit(LeaveModel model, string actionType)
        {
            if (Session["UID"] != null && actionType == "Submit")
            {
                LeaveTable leave = _repository.leavetable.Where(u => u.ID == model.ID).FirstOrDefault();
                leave.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", model.LeaveStartDate.Date));
                leave.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", model.LeaveEndDate.Date));
                leave.LeaveName = model.LeaveName.Trim();
                leave.LeaveType = model.LeaveType? "Leave":"Training";
                leave.DayType = model.DayType?"FullDay":"HalfDay";
                _repository.AddLeave(leave);
                if (_repository.SaveChanges())
                {
                    return RedirectToAction("ViewLeave");
                }
                else
                {
                    return View(model);
                }
            }
            else if (Session["UID"] != null && actionType == "Cancel")
            {
                return RedirectToAction("ViewLeave");
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }
        public ActionResult DeleteLeave(int id)
        {
            string uid = Session["UID"].ToString().Trim();
            LeaveTable leave = _repository.leavetable.Where(u => u.ID == id).FirstOrDefault();
            _repository.DeleteLeave(leave);
            if (_repository.SaveChanges())
            {
                return RedirectToAction("ViewLeave");
            }
            else
            {
                return View();
            }
        }

        public ActionResult LeaveHistory()
        {
            if (Session["UID"] != null)
            {
                string uid = Session["UID"].ToString().Trim();
                List<LeaveModel> lm = new List<LeaveModel>();
                IEnumerable<LeaveTable> leavetab = _repository.leavetable.Where(u => u.UserID == uid).ToList();
                foreach (LeaveTable items in leavetab)
                {
                    LeaveModel lmodel = new LeaveModel();
                    lmodel.ID = items.ID;
                    lmodel.LeaveStartDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveStartDate));
                    lmodel.LeaveEndDate = Convert.ToDateTime(String.Format("{0:d}", items.LeaveStartDate));
                    lmodel.LeaveName = items.LeaveName.Trim();
                    lmodel.LeaveType = string.Equals(items.LeaveType.Trim(), "Leave");
                    lmodel.DayType = string.Equals(items.DayType.Trim(), "FullDay");
                    lm.Add(lmodel);
                    lmodel.LeaveM = lm;
                }

                return View(lm);
            }
            else
            {
                return View();
            }
        }
    }
}
